package com.cosina.test;

import static javax.microedition.khronos.opengles.GL10.GL_COLOR_BUFFER_BIT;
import static javax.microedition.khronos.opengles.GL10.GL_DEPTH_BUFFER_BIT;
import static javax.microedition.khronos.opengles.GL10.GL_TEXTURE_2D;
import static javax.microedition.khronos.opengles.GL10.GL_TEXTURE_COORD_ARRAY;
import static javax.microedition.khronos.opengles.GL10.GL_VERTEX_ARRAY;

import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import java.util.Random;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.opengl.GLUtils;
import android.opengl.GLSurfaceView.Renderer;

public class Lession9Render implements Renderer{
	
	public static final int STAR_COUNT = 50;
	public Star[] stars = new Star[STAR_COUNT];
	
	float zoom = -15.0f;
	float tilt = 90.0f;
	float spin;
	int texture;
	
	@Override
	public void onSurfaceCreated(GL10 gl, EGLConfig config) {
		IntBuffer buffer = IntBuffer.allocate(1);
		gl.glGenTextures(1, buffer);
		texture = buffer.get();
		
		gl.glBindTexture(GL10.GL_TEXTURE_2D, texture);
		gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
		gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
		GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, DataManager.getBm(R.drawable.star), 0);
		
		gl.glEnable(GL10.GL_TEXTURE_2D);
		gl.glShadeModel(GL10.GL_SMOOTH);//TOSEE;
		
		gl.glClearColor(0f, 0f, 0f, 0.5f);
		gl.glClearDepthf(1.0f);//TOSEE;
		gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
		gl.glEnable(GL10.GL_BLEND);
		gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE);
		
		
		for(int i=0; i<STAR_COUNT; i++){
			Star star = new Star();
			star.dist = i + 0f; //TOSEE
			star.r = random.nextInt(256);
			star.g = random.nextInt(256);
			star.b = random.nextInt(256);
			stars[i] = star;
		}
		
	}
	Random random = new Random();
	
	int one = 0x10000;
	
	@Override
	public void onDrawFrame(GL10 gl) {
		gl.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	// Clear The Screen And The Depth Buffer
		gl.glBindTexture(GL_TEXTURE_2D, texture);		// Select Our Texture
		
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
		
		IntBuffer coord = IntBuffer.wrap(new int[]{
				0,0,
				one,0,
				one,one,
				0,one,
		});
		IntBuffer vertexs = IntBuffer.wrap(new int[]{
				-one,-one,0,
				one,-one,0,
				one,one,0,
				-one,one,0,
		});
		ByteBuffer indices = ByteBuffer.wrap(new byte[]{
				1, 0, 2, 3
		});
		
		gl.glVertexPointer(3, GL10.GL_FIXED, 0, vertexs);
		gl.glTexCoordPointer(2, GL10.GL_FIXED, 0, coord);
		gl.glLoadIdentity();
		
		for(int i=0; i<STAR_COUNT;i++){
			gl.glLoadIdentity();
			gl.glTranslatef(0.0f, 0.0f, zoom);
//			gl.glRotatef(tilt, 1.0f, 0.0f, 0.0f);
//			gl.glRotatef(stars[i].angle,0.0f,1.0f,0.0f);	// Rotate To The Current Stars Angle
//			gl.glTranslatef(stars[i].dist,0.0f,0.0f);	// Move Forward On The X Plane
//			gl.glRotatef(-stars[i].angle,0.0f,1.0f,0.0f);	// Cancel The Current Stars Angle
//			gl.glRotatef(-tilt,1.0f,0.0f,0.0f);		// Cancel The Screen Tilt
			
			
			Star some = stars[STAR_COUNT - i - 1];
			gl.glColor4f((float)some.r/255.0f,
					(float)some.g/255.0f,
					(float)some.b/255.0f, 1.0f);
			{
				coord.position(0);
				vertexs.position(0);
				indices.position(0);
				
				gl.glDrawElements(GL10.GL_TRIANGLE_STRIP, 4, GL10.GL_UNSIGNED_BYTE, indices);
			}
			
			gl.glRotatef(spin,0.0f,0.0f,1.0f);			// Rotate The Star On The Z Axis
			Star curr = stars[i];
			gl.glColor4f((float)curr.r/255.0f,
					(float)curr.g/255.0f,
					(float)curr.b/255.0f, 1.0f);
			
			{
				coord.position(0);
				vertexs.position(0);
				indices.position(0);
				gl.glDrawElements(GL10.GL_TRIANGLE_STRIP, 4, GL10.GL_UNSIGNED_BYTE, indices);
			}
			
//			spin+=0.01f;					// Used To Spin The Stars
//			curr.angle+=(i)/STAR_COUNT;		// Changes The Angle Of A Star
//			curr.dist-=0.01f;
//			
//			if (curr.dist<0.0f)			// Is The Star In The Middle Yet
//			{
//				curr.dist+=5.0f;			// Move The Star 5 Units From The Center
//				curr.r=random.nextInt(256);		// Give It A New Red Value
//				curr.g=random.nextInt(256);		// Give It A New Green Value
//				curr.b=random.nextInt(256);		// Give It A New Blue Value
//			}

		}
		gl.glDisableClientState(GL_TEXTURE_COORD_ARRAY);
		gl.glDisableClientState(GL_VERTEX_ARRAY);
	}
	
	@Override
	public void onSurfaceChanged(GL10 gl, int width, int height) {
		gl.glViewport(0, 0, width, height);

		float ratio = (float) width / height;
		gl.glMatrixMode(GL10.GL_PROJECTION);
		gl.glLoadIdentity();
		gl.glFrustumf(-ratio, ratio, -1, 1, 1, 10);
	}
}

class Star{
	int r, g, b;
	float dist;
	float angle = 0.0f;
}